package com.ruoyi.service;

import java.util.Collection;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson2.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.ruoyi.common.utils.StringUtils;

@Service
@Configuration
public class AliyunSmsServiceImpl implements ISmsService {

	@Value("${aliyun.accessKeyId}")
    private String accessKeyId;
	
    @Value("${aliyun.accessSecret}")
    private String accessSecret;
    
    @Value("${aliyun.signName}")
    private String signName;
    
    @Value("${aliyun.templateCode}")
    private String templateCode;

	@Override
	public String sendSms(Collection<String> phones, Map<String, String> params) throws Exception {
		//1:区域 2:阿里云key 3：阿里云密钥
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);  //设置请求方式
        request.setSysAction("SendSms");    //设置请求的类别
        request.setSysVersion("2017-05-25");    //设置参数版本号
        request.setSysDomain("dysmsapi.aliyuncs.com");  //设置请求域名
        request.putQueryParameter("RegionId", "cn-hangzhou"); //地区
        request.putQueryParameter("PhoneNumbers", StringUtils.join(phones, ","));    //发送给的手机号
        request.putQueryParameter("SignName", signName);     //在阿里云申请的签名名称
        request.putQueryParameter("TemplateCode", templateCode); //在阿里云申请的模板id
        request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); //在模板中设置的参数传递， value为json格式
        CommonResponse response = client.getCommonResponse(request);
        return response.getData();
	}

}
